/***************************************************************************
 *   oboe.controllers.NodeStateMachine                                     *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers
{
	import flash.utils.Dictionary;
	import flash.utils.describeType;
	
	import mx.states.State;
	
	import oboe.math.Mesh2D;
	
	public class NodeStateMachine extends NodeController
	{
		private static const globalStateList:Dictionary = new Dictionary();
		private var stateList:Dictionary;
		private var _state:String;
		private var nextState:String;
		private var nextStateArgs:Array;
		private var activeStateInfo:StateInfo;
		
		public function NodeStateMachine(mesh:Mesh2D)
		{
			super(mesh);
			setupStates();
		}
		
		protected function setState( newState:String, ... args ):void
		{
			this.nextState = newState;
			this.nextStateArgs = args;
		}
		
		protected function getState():String
		{
			return this._state;
		}
		
		protected function runState( ... args ):void
		{
			if( this.nextState != this._state )
			{
				if( this.activeStateInfo && this.activeStateInfo.exit != null ) this[this.activeStateInfo.exit]();
				trace( "changing to state", this.nextState );
				this.activeStateInfo = this.stateList[ this.nextState ];
				if( this.activeStateInfo && this.activeStateInfo.enter != null ) this[this.activeStateInfo.enter].apply( this, this.nextStateArgs );
				this._state = this.nextState;
			}
			if( this.activeStateInfo && this.activeStateInfo.run != null ) this[this.activeStateInfo.run].apply( this, args );
		}
		
		private function setupStates():void
		{
			var myClass:Class = Object(this).constructor;
			var stateList:Dictionary;
			if( !(stateList=globalStateList[myClass]) )
			{
				stateList = new Dictionary();
				globalStateList[myClass] = stateList;
				var xml:XMLList = describeType( myClass )..method;
				var len:int = xml.length();
				for( var i:int=0; i<len; ++i )
				{
					var name:String = String(xml.@name[i]);
					var si:StateInfo;
					if( name.substr(0, 6) == "state_" )
					{
						name = name.substr( 6 );
						if( !(si=stateList[ name ] ) )
						{
							si = stateList[ name ] = new StateInfo();
						}
						si.run = "state_" + name;
					}
					else if( name.substr(0, 6) == "enter_" )
					{
						name = name.substr( 6 );
						if( !(si=stateList[ name ] ) )
						{
							si = stateList[ name ] = new StateInfo();
						}
						si.enter = "enter_" + name;
					}
					else if( name.substr(0, 5) == "exit_" )
					{
						name = name.substr( 5 );
						if( !(si=stateList[ name ] ) )
						{
							si = stateList[ name ] = new StateInfo();
						}
						si.exit = "exit_" + name;
					}
				}				
			}
			this.stateList = stateList;
		}
	}
}

class StateInfo
{
	public var enter:String;
	public var run:String;
	public var exit:String;
}